/**
 * @param {number} num
 * @return {number}
 */
var maximumSwap = function (num) {
    let str = String(num).split('')
    let stack = []
    for (let i = 0; i < str.length; i++) {
        while (stack.length !== 0 && str[stack[stack.length - 1]] < str[i]) {
            stack.pop()
        }
        stack.push(i)
    }
    let i = 0
    let tempInx = 0
    stack.push(-1)
    while (i < stack.length - 1) {
        if (stack[i] !== i) {
            if(str[stack[i]] !== str[stack[i + 1]]) {
                let temp = str[stack[i]]
                str[stack[i]] = str[tempInx]
                str[tempInx] = temp
                break
            }
        } else {
            tempInx++
        }
        i++
    }
    return Number(str.join(''))
};
console.log(maximumSwap(2736));
console.log(maximumSwap(9973));
console.log(maximumSwap(1993));